/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ManageBLL;

import ManageDAL.DALManufacture;
import ManageDAL.DALProduct;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Date;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Pham
 */
public class BLLProduct {
    int productID;
    String productName;
    int productQuantity;
    String productImage;
    Date productDateUpdate;
    String productDescription;
    String productWarranty;
    String productPromotion;
    long productPrice;
    BLLManufacture bllManu;
    DALProduct dalPro;

    public BLLManufacture getBllManu() {
        return bllManu;
    }

    public void setBllManu(BLLManufacture bllManu) {
        this.bllManu = bllManu;
    }

    public long getProductPrice() {
        return productPrice;
    }

    public void setProductPrice(long productPrice) {
        this.productPrice = productPrice;
    }

    public int getProductID() {
        return productID;
    }

    public void setProductID(int productID) {
        this.productID = productID;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public int getProductQuantity() {
        return productQuantity;
    }

    public void setProductQuantity(int productQuantity) {
        this.productQuantity = productQuantity;
    }

    public String getProductImage() {
        return productImage;
    }

    public void setProductImage(String productImage) {
        this.productImage = productImage;
    }

    public Date getProductDateUpdate() {
        return productDateUpdate;
    }

    public void setProductDateUpdate(Date productDateUpdate) {
        this.productDateUpdate = productDateUpdate;
    }

    public String getProductDescription() {
        return productDescription;
    }

    public void setProductDescription(String productDescription) {
        this.productDescription = productDescription;
    }

    public String getProductWarranty() {
        return productWarranty;
    }

    public void setProductWarranty(String productWarranty) {
        this.productWarranty = productWarranty;
    }

    public String getProductPromotion() {
        return productPromotion;
    }

    public void setProductPromotion(String productPromotion) {
        this.productPromotion = productPromotion;
    }
    
    public BLLProduct()
    {
        dalPro = new DALProduct();
    }
    
    public BLLProduct(ResultSet rs) throws SQLException
    {
        DALManufacture dalManu = new DALManufacture();
        this.productID = rs.getInt("product_id");
        this.productName  = rs.getString("product_name");
        this.productQuantity = rs.getInt("product_quantity");
        this.productPrice = rs.getLong("product_price");
        this.productImage = rs.getString("product_image");
        this.productDateUpdate = rs.getDate("product_dateupdate");
        this.productDescription = rs.getString("product_description");
        this.productPromotion = rs.getString("product_promotion");
        this.productWarranty = rs.getString("product_warranty");
        this.bllManu = dalManu.getManuById(rs.getInt("manufacture_id"));
    }
    
    public DefaultTableModel getTbModel()
    {
        return dalPro.getDefaultTbModel();
    }
    
    //method get default table model of table product
    public DefaultTableModel getDTM()
    {
        try{
//            Vector col_name = new Vector();
//            Vector data_rows = new Vector();
            DefaultTableModel dtm = new DefaultTableModel();
            String[] col_names = {"Mã SP", "Tên SP","Hãng SX", "SL", "Giá", "Ngày cập nhật", "Bảo hành", "Khuyến mãi"};
            dtm.setColumnIdentifiers(col_names);
            for(BLLProduct itemPro : dalPro.getAllProduct())
            {
                dtm.addRow(new Object[]{itemPro.getProductID(), itemPro.getProductName(), itemPro.getBllManu().getManufactureName(),
                itemPro.getProductQuantity(), itemPro.getProductPrice(), itemPro.getProductDateUpdate(),
                itemPro.getProductWarranty(), itemPro.getProductPromotion()});
            }
            return dtm;
        }
        catch(Exception e)
        {
            return null;
        }
    }
    
    //method getProduct with the query
    public DefaultTableModel getProductbyQuery(String productName, String manu)
    {
        DefaultTableModel dtm = new DefaultTableModel();
        String[] col_names = {"Mã SP", "Tên SP","Hãng SX", "SL", "Giá", "Ngày cập nhật", "Bảo hành", "Khuyến mãi"};
        dtm.setColumnIdentifiers(col_names);
        for(BLLProduct itemPro : dalPro.getProductbyQuery(productName, manu))
        {
            dtm.addRow(new Object[]{itemPro.getProductID(), itemPro.getProductName(), itemPro.getBllManu().getManufactureName(),
            itemPro.getProductQuantity(), itemPro.getProductPrice(), itemPro.getProductDateUpdate(),
            itemPro.getProductWarranty(), itemPro.getProductPromotion()});
        }
        return dtm;
    }
    
    //method insert a row to table product
    public boolean exeInsertProduct(BLLProduct bllPro)
    {
        try
        {
            if(dalPro.insertRowProduct(bllPro))
                return true;
            return false;
        }
        catch(Exception ex)
        {
            System.err.print(ex.getMessage());
            return false;
        }
    }
    
    //method getProduct by ID
    public BLLProduct getProductByID(int Id)
    {
        return dalPro.getProductbyID(Id);
    }
    
    //method update a row to table product
    public boolean exeUpdateProduct(BLLProduct bllPro)
    {
        try
        {
            if(dalPro.updateProduct(bllPro))
                return true;
            return false;
        }
        catch(Exception ex)
        {
            System.err.print(ex.getMessage());
            return false;
        }
        
    }
    
    //method delete a product
    public boolean delProduct(int proID)
    {
        return dalPro.deleteProduct(proID);
    }
}
